/******************************************************************************
**                                                                           **
**                 Proprietary and Confidential Information                  **
**                                                                           **
**                 Copyright (c) Conexant Systems, Inc. Unpublished          **
**                                                                           **
** This source code and the algorithms implemented therein constitute        **
** confidential information and may compromise trade secrets of Conexant Systems,    **
** Inc. or its associates, and any unauthorized use thereof is prohibited.   **
**                                                                           **
******************************************************************************/

#ifndef _dc_jpeg_api_h_
#define _dc_jpeg_api_h_

/******************************************************************************
*  FILE FORMAT:
*      1. INCLUDES 
*      2. ENUMERATIONS 
*      3. DEFINES
*      4. STRUCTURES
*      5. GLOBALS
*      6. PRIVATE FUNCTION PROTOTYPES
*      7. FUNCTIONS
******************************************************************************/

/******************************************************************************
*  1. INCLUDES
******************************************************************************/
#ifndef WIN32

#include "dc_type.h"
#include "dc_return_codes.h"
#ifdef LINUX
#include "ospl.h"
#else
#include "tx_api.h"
#endif

#endif
/******************************************************************************
*  2. ENUMERATIONS
******************************************************************************/
typedef enum {
  DC_JPEGPIPE_ERROR_NONE    = 0x00,
  DC_JPEGPIPE_ERROR_MEM     = (1<<0),
  DC_JPEGPIPE_ERROR_OP      = (1<<1),
  DC_JPEGPIPE_ERROR_CM      = (1<<2),
  DC_JPEGPIPE_ERROR_CM_IP   = (1<<3),
  DC_JPEGPIPE_ERROR_CM_RO   = (1<<4),
  DC_JPEGPIPE_ERROR_CM_JP   = (1<<5),
  DC_JPEGPIPE_ERROR_CM_JH   = (1<<6),
  DC_JPEGPIPE_ERROR_STREAM  = (1<<7)
} DC_JPEGPIPE_ERROR_t;

typedef enum {
  DC_JPEGPIPE_PROFILE_HIGH=0,
  DC_JPEGPIPE_PROFILE_LOW=1,
  DC_JPEGPIPE_PROFILE_MEDIUM=2
} DC_JPEGPIPE_PROFILE_t;

typedef enum {
  DC_JPEGPIPE_STATUS_IDLE     = 0x00,
  DC_JPEGPIPE_STATUS_CREATED  = 0x01,
  DC_JPEGPIPE_STATUS_BUSY     = 0x02,
  DC_JPEGPIPE_STATUS_MEM_WAIT = 0x04,
  DC_JPEGPIPE_STATUS_SYS_DMB_LIMIT = DC_JPEGPIPE_STATUS_MEM_WAIT,
  DC_JPEGPIPE_STATUS_MAX_DMB_LIMIT = 0x08
} DC_JPEGPIPE_STATUS_t;

typedef enum {
  DC_JPEGPIPE_MODE_ENCODE=0,
  DC_JPEGPIPE_MODE_DECODE,
  DC_JPEGPIPE_MODE_SCREEN_HEADER,
  DC_JPEGPIPE_MODE_SCREEN_ALL
} DC_JPEGPIPE_MODE_t;

typedef enum {
  DC_JPEGPIPE_SAMPLE_MODE_411=0,
  DC_JPEGPIPE_SAMPLE_MODE_111,
  DC_JPEGPIPE_SAMPLE_MODE_211,
  DC_JPEGPIPE_SAMPLE_MODE_211V,
  DC_JPEGPIPE_SAMPLE_MODE_K,
  DC_JPEGPIPE_SAMPLE_MODE_UNSUPPORT
} DC_JPEGPIPE_SAMPLE_MODE_t;

typedef enum {
  DC_JPEGPIPE_QUANT_LEVEL_MEDIUM=0,
  DC_JPEGPIPE_QUANT_LEVEL_LOWER,
  DC_JPEGPIPE_QUANT_LEVEL_LOW,
  DC_JPEGPIPE_QUANT_LEVEL_HIGH,
  DC_JPEGPIPE_QUANT_LEVEL_HIGHER,
  DC_JPEGPIPE_QUANT_LEVEL_LOWEST
} DC_JPEGPIPE_QUANT_LEVEL_t;

typedef enum {
  DC_JPEGPIPE_CSC_MODE_NONE =0,
  DC_JPEGPIPE_CSC_MODE_DEFAULT,
  DC_JPEGPIPE_CSC_MODE_CUSTOM
} DC_JPEGPIPE_CSC_MODE_t;

#ifndef WIN32

#include "dc_jpeg_ctx_api.h"

/******************************************************************************
*  3. DEFINES
******************************************************************************/
#define JPEGPIPE_DEFAULT_STACK_BYTES   (16*1024)   // thread stack

#define JPEGPIPE_THREAD_NAME_LENGTH         8

#define MAX_JPEGPIPE_FIX_MARKER_DATA_COUNT  64
#define MIN_JPEGPIPE_MARKER_DATA_COUNT      5
#define MAX_JPEGPIPE_MARKER_DATA_COUNT      0xFFFF  // 65535

/******************************************************************************
*  4. STRUCTURES
******************************************************************************/
typedef struct DC_JPEGPIPE_RESULTS_STRUCT {
  
  DC_JPEGPIPE_ERROR_t error;

  struct {
    DC_JPEGPIPE_SAMPLE_MODE_t sample_mode;
    UINT32 n_width;
    UINT32 n_height;
    UINT32 n_components;
    UINT32 n_restart_interval;
  } jpeg;

} DC_JPEGPIPE_RESULTS_t;


typedef struct {
#ifdef LINUX	
  OSPL_THREAD control;
#else
  TX_THREAD control;
#endif  
  char name[JPEGPIPE_THREAD_NAME_LENGTH + 5];
  UINT32 stack[JPEGPIPE_DEFAULT_STACK_BYTES/4];
} DC_JPEGPIPE_THREAD_t;

typedef struct {
  DC_JPEGPIPE_PROFILE_t profile;

  DC_JPEGPIPE_MODE_t op_mode;

  DC_IMGBUF *ptr_imgbuf;
  DC_DMBIMG *ptr_dmbimg;
  UINT32 free_input;

  UINT32 top_left_x;
  UINT32 top_left_y;
  UINT32 bottom_right_x;
  UINT32 bottom_right_y;

  DC_JPEGPIPE_QUANT_LEVEL_t quant_level;
  UINT32 restart_interval;

  UINT32 n_app_marker_bytes;
  UINT32 app_fixed_marker;
  UINT8 app_marker_data[MAX_JPEGPIPE_FIX_MARKER_DATA_COUNT];

  DC_JPEGPIPE_CSC_MODE_t csc_mode;
  DC_CONIP_CSC_TABLE_DATA *csc_table_data;

  UINT32 n_trc_tables;
  UINT8 trc_data[3][65];

  UINT32 n_max_dmbs;

  UINT32 partial_decode_disabled;

  void (*statusCallback)(void *, DC_JPEGPIPE_STATUS_t status);
  void *status_callback_arg;

} DC_JPEGPIPE_API_t;

struct DC_JPEGPIPE_CONTEXT_STRUCT
{
  DC_JPEGPIPE_API_t api;

  DC_JPEGPIPE_STATUS_t status;
  DC_JPEGPIPE_ERROR_t error;

  JPEG__MEM_t mem;

  void (*completionCallback)(void *, DC_JPEGPIPE_RESULTS_t *);
  void *callback_arg;
  DC_JPEGPIPE_RESULTS_t *callback_results;

  DC_JPEGPIPE_SAMPLE_MODE_t sample_mode;

  DC_JPEGPIPE_THREAD_t thread;
#ifdef LINUX  
  OSPL_EVENT_FLAGS_GROUP event_flags;
#else
  TX_EVENT_FLAGS_GROUP event_flags;
#endif  
  UINT32 calls_flag;
  UINT32 signature;

  DC_JPEGPIPE_THREAD_t sub_thread;
#ifdef LINUX    
  OSPL_EVENT_FLAGS_GROUP sub_event_flags;
#else
  TX_EVENT_FLAGS_GROUP sub_event_flags;
#endif  

  DC_JPEG_CTX_t ctx;
};

typedef struct DC_JPEGPIPE_CONTEXT_STRUCT DC_JPEGPIPE_CONTEXT_t;


/******************************************************************************
*  5. GLOBALS
******************************************************************************/

/******************************************************************************
*  6. FUNCTION PROTOTYPES
******************************************************************************/

/*****************************************************************************/
/*  @brief      for client to begin set-up of jpeg work
 *
 *  @ingroup    JPEG
 *  @b function dc_jpegpipe_setup_begin
 *
 *  @param      pContext - pointer to DC_JPEGPIPE_CONTEXT_t structure
 *
 *  @return     DC_SUCCESS, DC_FAILURE, DC_BAD_ARGUMENT
 *
 *****************************************************************************/
DC_RETURN_CODE dc_jpegpipe_setup_begin 
 (DC_JPEGPIPE_CONTEXT_t *pContext);

/*****************************************************************************/
/*  @brief      for client to finish set-up of jpeg work
 *
 *  @ingroup    JPEG
 *  @b function dc_jpegpipe_setup_end
 *
 *  @param      pContext - pointer to DC_JPEGPIPE_CONTEXT_t structure
 *  @param      pMemParams - pointer to DC_MEM_PARAMS_t structure
 *
 *  @return     DC_SUCCESS, DC_FAILURE, DC_BAD_ARGUMENT
 *
 *****************************************************************************/
DC_RETURN_CODE dc_jpegpipe_setup_end
 (DC_JPEGPIPE_CONTEXT_t *pContext,
  DC_MEM_PARAMS_t *pMemParams);

/*****************************************************************************/
/*  @brief      for client to create and start a JPEG thread
 *
 *  @ingroup    JPEG
 *  @b function dc_jpegpipe_create
 *
 *  @param      pContext - pointer to DC_JPEGPIPE_CONTEXT_t structure
 *  @param      pMemParams - pointer to DC_MEM_PARAMS_t structure
 *  @param      name - pointer to thread name string
 *  @param      priority - Thread priority
 *  @param      completionCallback - function pointer to callback
 *  @param      callback_arg - pointer to first argument of callback
 *  @param      callback_results - pointer to second arg. of callback for result
 *  @return     DC_SUCCESS, DC_FAILURE, DC_BAD_ARGUMENT
 *
 *****************************************************************************/
DC_RETURN_CODE dc_jpegpipe_create
 (DC_JPEGPIPE_CONTEXT_t *pContext,
  DC_MEM_PARAMS_t *pMemParams,
  char *name,
  DC_THREAD_PRIORITY priority,
  void (*completionCallback)(void *arg, DC_JPEGPIPE_RESULTS_t *result),
  void *callback_arg,
  DC_JPEGPIPE_RESULTS_t *callback_results);

/*****************************************************************************/
/*  @brief      for client to cancel the JPEG thread
 *
 *  @ingroup    JPEG
 *  @b function dc_jpegpipe_cancel
 *
 *  @param      pContext - pointer to DC_JPEGPIPE_CONTEXT_t structure
 *  @return     DC_SUCCESS, DC_FAILURE, DC_BAD_ARGUMENT
 *
 *****************************************************************************/
DC_RETURN_CODE dc_jpegpipe_cancel
 (DC_JPEGPIPE_CONTEXT_t *pContext);

/*****************************************************************************/
/*  @brief      for client to delete the JPEG thread
 *
 *  @ingroup    JPEG
 *  @b function dc_jpegpipe_delete
 *
 *  @param      pContext - pointer to DC_JPEGPIPE_CONTEXT_t structure
 *  @return     DC_SUCCESS, DC_FAILURE, DC_BAD_ARGUMENT
 *
 *****************************************************************************/
DC_RETURN_CODE dc_jpegpipe_delete
 (DC_JPEGPIPE_CONTEXT_t *pContext);

/*****************************************************************************/
/*  @brief      for client to get the status of the JPEG thread
 *
 *  @ingroup    JPEG
 *  @b function dc_jpegpipe_status_get
 *
 *  @param      pContext - pointer to DC_JPEGPIPE_CONTEXT_t structure
 *  @param      status - pointer to DC_JPEGPIPE_STATUS_t structure
 *  @param      error - pointer to DC_JPEGPIPE_ERROR_t structure
 *  @return     DC_SUCCESS, DC_FAILURE, DC_BAD_ARGUMENT
 *
 *****************************************************************************/
DC_RETURN_CODE dc_jpegpipe_status_get
 (DC_JPEGPIPE_CONTEXT_t *pContext,
  DC_JPEGPIPE_STATUS_t  *status,
  DC_JPEGPIPE_ERROR_t *error);

/*****************************************************************************/
/*  @brief      for client to specify profile
 *
 *  @ingroup    JPEG
 *  @b function dc_jpegpipe_setup_profile_set
 *
 *  @param      pContext - pointer to DC_JPEGPIPE_CONTEXT_t structure
 *  @param      profile - HIGH / LOW
 *  @return     DC_SUCCESS, DC_FAILURE, DC_BAD_ARGUMENT
 *
 *****************************************************************************/
DC_RETURN_CODE dc_jpegpipe_setup_profile_set 
 (DC_JPEGPIPE_CONTEXT_t *pContext,
  DC_JPEGPIPE_PROFILE_t profile);

/*****************************************************************************/
/*  @brief      for client to specify one of operation modes
 *
 *  @ingroup    JPEG
 *  @b function dc_jpegpipe_setup_mode_set
 *
 *  @param      pContext - pointer to DC_JPEGPIPE_CONTEXT_t structure
 *  @param      mode - operation mode
 *  @return     DC_SUCCESS, DC_FAILURE, DC_BAD_ARGUMENT
 *
 *****************************************************************************/
DC_RETURN_CODE dc_jpegpipe_setup_mode_set 
 (DC_JPEGPIPE_CONTEXT_t *pContext,
  DC_JPEGPIPE_MODE_t mode);

/*****************************************************************************/
/*  @brief      for client to specify input and output containers
 *
 *  @ingroup    JPEG
 *  @b function dc_jpegpipe_setup_data_set
 *
 *  @param      pContext - pointer to DC_JPEGPIPE_CONTEXT_t structure
 *  @param      imgbuf - pointer to DC_IMGBUF
 *  @param      dmbimg - pointer to DC_DMBIMG
 *  @param      free_input - whether or not to free input
 *  @return     DC_SUCCESS, DC_FAILURE, DC_BAD_ARGUMENT
 *
 *****************************************************************************/
DC_RETURN_CODE dc_jpegpipe_setup_data_set
 (DC_JPEGPIPE_CONTEXT_t *pContext, 
  DC_IMGBUF *imgbuf, 
  DC_DMBIMG *dmbimg, 
  UINT32 free_input);

/*****************************************************************************/
/*  @brief      for client to specify ROI of input for decode
 *
 *  @ingroup    JPEG
 *  @b function dc_jpegpipe_setup_crop_set
 *
 *  @param      pContext - pointer to DC_JPEGPIPE_CONTEXT_t structure
 *  @param      top_left_x - x position of top & left pixel, included
 *  @param      top_left_y - y position of top & left pixel, included
 *  @param      bottom_right_x  - x position of bottom & right pixel, included
 *  @param      bottom_right_y - y position of bottom & right pixel, included
 *  @return     DC_SUCCESS, DC_FAILURE, DC_BAD_ARGUMENT
 *
 *****************************************************************************/
DC_RETURN_CODE dc_jpegpipe_setup_crop_set
 (DC_JPEGPIPE_CONTEXT_t *pContext, 
  UINT32 top_left_x,
  UINT32 top_left_y,
  UINT32 bottom_right_x,
  UINT32 bottom_right_y );

/*****************************************************************************/
/*  @brief      for client to specify sample mode of encode
 *
 *  @ingroup    JPEG
 *  @b function dc_jpegpipe_setup_sample_mode_set
 *
 *  @param      pContext - pointer to DC_JPEGPIPE_CONTEXT_t structure
 *  @param      sample_mode - encode sample mode
 *  @return     DC_SUCCESS, DC_FAILURE, DC_BAD_ARGUMENT
 *
 *****************************************************************************/
DC_RETURN_CODE dc_jpegpipe_setup_sample_mode_set
 (DC_JPEGPIPE_CONTEXT_t *pContext, 
  DC_JPEGPIPE_SAMPLE_MODE_t sample_mode);

/*****************************************************************************/
/*  @brief      for client to specify quantization level of encode
 *
 *  @ingroup    JPEG
 *  @b function dc_jpegpipe_setup_quant_level_set
 *
 *  @param      pContext - pointer to DC_JPEGPIPE_CONTEXT_t structure
 *  @param      quant_level - encode quantization level
 *  @return     DC_SUCCESS, DC_FAILURE, DC_BAD_ARGUMENT
 *
 *****************************************************************************/
DC_RETURN_CODE dc_jpegpipe_setup_quant_level_set
 (DC_JPEGPIPE_CONTEXT_t *pContext, 
  DC_JPEGPIPE_QUANT_LEVEL_t quant_level);

/*****************************************************************************/
/*  @brief      for client to specify restart interval of RST for encode
 *
 *  @ingroup    JPEG
 *  @b function dc_jpegpipe_setup_restart_interval_set
 *
 *  @param      pContext - pointer to DC_JPEGPIPE_CONTEXT_t structure
 *  @param      restart_interval_mcu_lines - # of MCU-lines
 *  @return     DC_SUCCESS, DC_FAILURE, DC_BAD_ARGUMENT
 *
 *****************************************************************************/
DC_RETURN_CODE dc_jpegpipe_setup_restart_interval_set
 (DC_JPEGPIPE_CONTEXT_t *pContext, 
  UINT32 restart_interval_mcu_lines);

/*****************************************************************************/
/*  @brief      for client to specify application marker to insert in for encode
 *
 *  @ingroup    JPEG
 *  @b function dc_jpegpipe_setup_app_marker_set
 *
 *  @param      pContext - pointer to DC_JPEGPIPE_CONTEXT_t structure
 *  @param      marker_data - pointer to marker data 
 *  @param      n_marker_bytes - # of app marker data including marker&length
 *  @return     DC_SUCCESS, DC_FAILURE, DC_BAD_ARGUMENT
 *
 *****************************************************************************/
DC_RETURN_CODE dc_jpegpipe_setup_app_marker_set
 (DC_JPEGPIPE_CONTEXT_t *pContext,
  UINT8 *marker_data,
  UINT32 n_marker_bytes);

/*****************************************************************************/
/*  @brief      for client to specify color table 
 *
 *  @ingroup    JPEG
 *  @b function dc_jpegpipe_setup_color_table_set
 *
 *  @param      pContext - pointer to DC_JPEGPIPE_CONTEXT_t structure
 *  @param      csc_mode - specify one of color table modes
 *  @param      csc_table_data - pointer to custom csc table data
 *  @return     DC_SUCCESS, DC_FAILURE, DC_BAD_ARGUMENT
 *
 *****************************************************************************/
DC_RETURN_CODE dc_jpegpipe_setup_color_table_set
 (DC_JPEGPIPE_CONTEXT_t *pContext,
  DC_JPEGPIPE_CSC_MODE_t csc_mode,
  DC_CONIP_CSC_TABLE_DATA *csc_table_data);

/*****************************************************************************/
/*  @brief      for client to specify trc table for decode
 *
 *  @ingroup    JPEG
 *  @b function dc_jpegpipe_setup_trc_table_set
 *
 *  @param      pContext - pointer to DC_JPEGPIPE_CONTEXT_t structure
 *  @param      trc_table - pointer to trc
 *  @return     DC_SUCCESS, DC_FAILURE, DC_BAD_ARGUMENT
 *
 *****************************************************************************/
DC_RETURN_CODE dc_jpegpipe_setup_trc_table_set
 (DC_JPEGPIPE_CONTEXT_t *pContext, 
  UINT8 *trc_table[3]);

/*****************************************************************************/
/*  @brief      for client to specify a status callback
 *
 *  @ingroup    JPEG
 *  @b function dc_jpegpipe_setup_status_callback_set
 *
 *  @param      pContext - pointer to DC_JPEGPIPE_CONTEXT_t structure
 *  @param      statusCallback - function pointer to callback
 *  @param      callback_arg - pointer to first argument of callback
 *  @return     DC_SUCCESS, DC_FAILURE, DC_BAD_ARGUMENT
 *
 *****************************************************************************/
DC_RETURN_CODE dc_jpegpipe_setup_status_callback_set
 (DC_JPEGPIPE_CONTEXT_t *pContext,
  void (*statusCallback)(void *arg, DC_JPEGPIPE_STATUS_t status),
  void *callback_arg);

/*****************************************************************************/
/*  @brief      for client to specify a maximum number of DMBs allowed for encode
 *
 *  @ingroup    JPEG
 *  @b function dc_jpegpipe_setup_max_dmb_set
 *
 *  @param      pContext - pointer to DC_JPEGPIPE_CONTEXT_t structure
 *  @param      n_max_dmbs - # of max dmbs, no limit as default if zero
 *  @return     DC_SUCCESS, DC_FAILURE, DC_BAD_ARGUMENT
 *
 *****************************************************************************/
DC_RETURN_CODE dc_jpegpipe_setup_max_dmb_set
 (DC_JPEGPIPE_CONTEXT_t *pContext,
  UINT32 n_max_dmbs);

DC_RETURN_CODE dc__jpegpipe_setup_data_range_invert
 (DC_JPEGPIPE_CONTEXT_t *pContext);

DC_RETURN_CODE dc__jpegpipe_setup_decode_line_mode_set
 (DC_JPEGPIPE_CONTEXT_t *pContext, boolean enable);

DC_RETURN_CODE dc__jpegpipe_setup_decode_partial_mode_disable
 (DC_JPEGPIPE_CONTEXT_t *pContext);

#endif  // WIN32
#endif
